<?php
namespace app\admin\controller;

use app\admin\model\Event as EventModel;
use app\admin\model\Menu as MenuModel;
use think\Db;
use think\Request;

class Index extends Base
{
    public function index()
    {
        $s_menu_id = \think\Session::get('menu_id');
        $pieces = explode(",", $s_menu_id);
        $menu_data = \app\admin\model\Menu::menuData();

        foreach ($menu_data as $k=>$v){
            if(!empty($v["child"])){
                foreach ($v["child"] as $a=>$b){
                    if(!(in_array($b["id"],$pieces)) ){/*&& ($b["id"] != 28) && ($b["id"] != 29)*/
                        unset($menu_data[$k]["child"][$a]);
                    }
                    $menu_data[$k]["url"] = $v["child"][0]["url"];
                }
            }
        }

        foreach ($menu_data as $k=>$v){
            if(empty($v["child"])){/*&& ($v["id"] != 10)*/
                unset($menu_data[$k]);
            }
        }

        $this->assign('menu',$menu_data);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }
    public function index1()
    {
        $model = new EventModel;
        //渲染页面模板
        $result = $model->field('id,name,detail,happened_at, result,handler,created_at,remark')
            ->order('id','desc')
            //->select();
            ->paginate(10);

        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();

        //接收ajax传递的值
        $request = request();
        $ispost = $request->isPost();
        if($ispost){
            $id = $_POST["viewId"];
            $view = $model->getAll()->where("id",$id)->select();
            return json([
                'code' => 200,
                'view'=>$view,
            ]);
        }


        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('event',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }
}